/***************************************************************************
 *   Copyright (C) 2011 by The rPem3d developers                           *
 *   elseifkk@users.sf.net                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef ANALOGMETER_H
#define ANALOGMETER_H

#include <math.h>

#include <qwidget.h>

#define DEG2RAD(x) ((x)/180.*M_PI)

int const window_height=200;
int const window_width=200;
int const window_x=-window_width/2;
int const window_y=-window_height/2;
QRect const wRect(window_x,window_y,window_width,window_height);
int const frame_mergin_x=6;//40;
int const frame_mergin_y=6;//40;
int const frame_height=window_height-frame_mergin_y*2;
int const frame_width=window_width-frame_mergin_x*2;
int const unit_offset=frame_height/4;
int const long_tick = 8;//42;
int const short_tick = 2;//18;
int const frame_y=-frame_height/2;
int const frame_x=-frame_width/2;
int const arrow_width = 2;//14;
QColor const measure_col="dodgerblue";
QColor const arrow_col_in="indianred";
QColor const arrow_col_out="darkred";
QColor const border_col1="royalblue";
QColor const border_col2="darkslateblue";
QColor const caption_col="lightsteelblue";
QColor const unit_col="powderblue";
QColor const maxval_col="lightsalmon";
QColor const label_col="powderblue";
int const unit_font_size=18;
int const label_font_size=13;
int const caption_font_size=14;
int const maxval_font_size=16;

double const measure_len=270.;
double const  measure_start       =(180.-(double)measure_len)/2.;
double const  measure_begin       =180+measure_start;
double const  measure_len_trans   =measure_len*16;// deg*16;
double const measure_start_trans =measure_start*16;

int const num_tick_def=8;
int const num_tick_div_def=4;
double const tick_step_def=10.;

class AnalogMeter : public QWidget {
    Q_OBJECT

public:
    AnalogMeter( QWidget *parent=0, const char *name=0);
    void initAnalogMeter(QString u="", QString c="",int ticks=num_tick_def, double step=tick_step_def, int tick_div=num_tick_div_def);
    void updateMeter(double);
    void setMeasureGeom();
    void scaleMeasure(int);
    void resetMax();

private:

    int num_tick;
    int num_tick_div;
    int num_tick_short;
    double tick_deg;
    double tick_deg_short;
    double tick_step;
    QPixmap *pm;

    double vmax, vmin;
    double vold;
    QString caption;
    QString unit;
    double maxval;
    double lastDrawAngle;
    double currentAngle;
    double vtod(double);
    void drawBg();
    void drawMaxVal(QPainter*);
    void drawTitle(QPainter*);
    void drawMeter( QPainter* );
    void drawMeasure(QPainter*);
    void drawBorder(QPainter*);
    void drawArrow(QPainter*);
    void drawLabel(QPainter*);
    double lastAngle;

protected:
    void paintEvent( QPaintEvent *);
    void mousePressEvent(QMouseEvent *);
};


#endif // AMETER_H
